#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <fcntl.h>
#include "keycode.h"

/*
 * 存取摄像机,监视器
 */
TDeviceInfo	monitor[MONITOR_COUNT];
TDeviceInfo	camera[CAMERA_COUNT];
/*
 * 摄像机,监视器长度
 */
static int camera_len = 0;
static int monitor_len = 0;


/*
 * open file of config
 */
FILE* open_file(char * file_name)
{
	FILE *fp;

	fp = fopen(file_name, "r");
	if (!fp) {
		printf("file [%s] open error!\n", file_name);
		exit(1);
	}

	return fp;
}

/*
 * read file of *.cfg, fill struct TDeviceInfo.
 */
int get_device_param(unsigned char * file, TDeviceInfo * device)
{
	FILE *fp;	
	char buf[512];
	int count = 0, in = 0;
	char *pdata[128];
	char *pbuf;
	TDeviceInfo *pdevice = device;
//	printf("------- get_device_param ------\n");
	fp = open_file(file);
	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		memset(pdata, 0, sizeof(pdata));
		fscanf(fp, "%s", buf);
		if (buf[0] == '#'||buf[0] == ' ')
			continue;

		if (strlen(buf) < 4)
			continue;

		pbuf = buf;
		in = 0;
		while ((pdata[in] = strtok(pbuf, ",")) != NULL) {
			in++;
			pbuf = NULL; 
		}

		if (pdata[1] == NULL)
			continue;

		pdevice[count].Id		= atoi(pdata[0]);
		pdevice[count].DeviceNo = atoi(pdata[1]);
		strcpy(pdevice[count].IP, pdata[2]);
		pdevice[count].Port = atoi(pdata[3]); 
		pdevice[count].ChanNum = atoi(pdata[4]);
		strcpy(pdevice[count].UserName, pdata[5]);
		strcpy(pdevice[count].Password, pdata[6]);

		count++;
	}
	fclose(fp);

	return count;
}

/*
 * 初始化,从配置文件取数据,只需运行一次.
 */
int init_camera_monitor(void)
{
	memset(monitor, 0, sizeof monitor);
	memset(camera, 0, sizeof camera);
	monitor_len = get_device_param(MON_FILE,monitor);
	camera_len = get_device_param(CAM_FILE, camera);
}

/*
 * 搜索设备ID,返回相应结构.
 *	num  : 设备ID
 *  type : MONITOR || CAMERA
 */
TDeviceInfo *search(unsigned int num, TYPE type)
{
	TDeviceInfo *pdevice;
	unsigned int count = 0;
	unsigned int i;

	if (type == MONITOR) {
		for (pdevice = monitor, i = 0; i < monitor_len; i++,pdevice++) {
			if (pdevice->DeviceNo == num)
				return pdevice;
		}
	}
	else if (type == CAMERA) {
		for (pdevice = camera, i = 0; i < camera_len; i++,pdevice++) {
			if (pdevice->DeviceNo == num)
				return pdevice;
		}
	}
	
	return NULL;
}

/*
 * 搜索ID号
 */
TDeviceInfo *search_id(unsigned int id, TYPE type)
{
	TDeviceInfo *pdevice;
	unsigned int i;

	if (type == MONITOR) {
		for (pdevice = monitor, i = 0; i < monitor_len; i++,pdevice++) {
			if (pdevice->Id == id)
				return pdevice;
		}
	}
	else if (type == CAMERA) {
		for (pdevice = camera, i = 0; i < camera_len; i++,pdevice++) {
			if (pdevice->Id == id)
				return pdevice;
		}
	}
	
	return NULL;
}

/*
 * 通过 ID 号找到监视器号
 */
int GetMonitorNoByID(int id)
{
	TDeviceInfo *pdevice;
	unsigned int i;

	for (pdevice = monitor, i = 0; i < monitor_len; i++,pdevice++) {
		if (pdevice->Id == id)
			return pdevice->DeviceNo;
	}
	
	return -1;
}

/*
 * 通过 ID 号找到摄像机号
 */
int GetCameraNoByID(int id)
{
	TDeviceInfo *pdevice;
	unsigned int i;

	for (pdevice = camera, i = 0; i < camera_len; i++,pdevice++) {
		if (pdevice->Id == id)
			return pdevice->DeviceNo;
	}
	
	return -1;
}

/*
 * 打印设备结构信息
 */
void print_device_info(TDeviceInfo *pdevice)
{
	if (pdevice) {
		printf("Id		 = %d\n", pdevice->Id);
		printf("DeviceNo = %d\n", pdevice->DeviceNo);
		printf("IP		 = %s\n", pdevice->IP);
		printf("Port	 = %d\n", pdevice->Port);
		printf("ChanNum	 = %d\n", pdevice->ChanNum);
		printf("UserName = %s\n", pdevice->UserName);
		printf("Password = %s\n\n", pdevice->Password);
	}
}

#if 0	/* DEBUG */

int main()
{
	init_camera_monitor();
	print_device_info(monitor);
	print_device_info(camera);
}
#endif

#if 0
/*
 * 从文件读取配置信息,填充TDeviceInfo结构.
 */
int fill_struct_deviceinfo(unsigned char * file, TDeviceInfo * device)
{
	TDeviceInfo	*pdevice = device;

	return get_device_param(file, pdevice);	
}

/*
 * 搜索设备ID,返回相应结构.
 *	num  : 设备ID
 *  type : MONITOR || CAMERA
 */
TDeviceInfo *search(unsigned int num, TYPE type)
{
	TDeviceInfo	monitor[MONITOR_COUNT];
	TDeviceInfo	camera[CAMERA_COUNT];
	TDeviceInfo *pdevice;
	unsigned int count = 0;
	unsigned int i;

	if (type == MONITOR) {
		memset(monitor, 0, sizeof monitor);
		count = fill_struct_deviceinfo(MON_FILE, monitor);

		for (pdevice = monitor, i = 0; i < count; i++,pdevice++) {
			if (pdevice->DeviceNo == num)
				return pdevice;
		}
	}
	else if (type == CAMERA) {
		memset(camera, 0, sizeof camera);
		count = fill_struct_deviceinfo(CAM_FILE, camera);

		for (pdevice = camera, i = 0; i < count; i++,pdevice++) {
			if (pdevice->DeviceNo == num)
				return pdevice;
		}
	}
	
	return NULL;
}

/*
 * 搜索ID号
 */
TDeviceInfo *search_id(unsigned int id, TYPE type)
{
	TDeviceInfo	monitor[MONITOR_COUNT];
	TDeviceInfo	camera[CAMERA_COUNT];
	TDeviceInfo *pdevice;
	unsigned int count = 0;
	unsigned int i;

	if (type == MONITOR) {
		memset(monitor, 0, sizeof monitor);
		count = fill_struct_deviceinfo(MON_FILE, monitor);

		for (pdevice = monitor, i = 0; i < count; i++,pdevice++) {
			if (pdevice->Id == id)
				return pdevice;
		}
	}
	else if (type == CAMERA) {
		memset(camera, 0, sizeof camera);
		count = fill_struct_deviceinfo(CAM_FILE, camera);

		for (pdevice = camera, i = 0; i < count; i++,pdevice++) {
			if (pdevice->Id == id)
				return pdevice;
		}
	}
	
	return NULL;
}

/*
 * 通过 ID 号找到监视器号
 */
int GetMonitorNoByID(int id)
{
	TDeviceInfo	monitor[MONITOR_COUNT];
	TDeviceInfo *pdevice;
	unsigned int count = 0;
	unsigned int i;

	memset(monitor, 0, sizeof monitor);
	count = fill_struct_deviceinfo(MON_FILE, monitor);

	for (pdevice = monitor, i = 0; i < count; i++,pdevice++) {
		if (pdevice->Id == id)
			return pdevice->DeviceNo;
	}
	
	return -1;
}

/*
 * 通过 ID 号找到摄像机号
 */
int GetCamertaNoByID(int id)
{
	TDeviceInfo	camera[CAMERA_COUNT];
	TDeviceInfo *pdevice;
	unsigned int count = 0;
	unsigned int i;

	memset(camera, 0, sizeof camera);
	count = fill_struct_deviceinfo(CAM_FILE, camera);

	for (pdevice = camera, i = 0; i < count; i++,pdevice++) {
		if (pdevice->Id == id)
			return pdevice->DeviceNo;
	}
	
	return -1;
}
#endif

#if 0
int _main()
{
	TDeviceInfo *pdevice = NULL;
	TDeviceInfo	monitor[MONITOR_COUNT];
	unsigned int monitor_all_count = 0;	/* 监视器总数 */

while(1) {
	memset(monitor, 0, sizeof monitor);
	monitor_all_count = fill_struct_deviceinfo(MON_FILE, monitor);
	printf("------- monitor_all_count:%d ------\n", monitor_all_count);
}

	return 0;
}

#endif

